﻿#region Copyright © 2013 Paul Miller (Millz)
// All rights are reserved. Reproduction or transmission in whole or in part,
// any form or by any means, electronic, mechanical or otherwise, is prohibited
// without the prior written consent of the copyright owner.
// 
// Filename: ConfigurationForm.cs
// Date:     23/09/2013
// Author:   Paul Miller (Millz)
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Insanity.Helpers;
using Insanity.Settings;
using Styx.Common;

namespace Insanity.GUI
{
    public partial class ConfigurationForm : Form
    {
        private static InsanitySettings Settings { get { return InsanitySettings.Instance; } }

        readonly List<string> _introImage = new List<string>();

        #region Change Log

        public const string ChangeLogText = @"[27/Oct/2013]
- Released
";

        #endregion

        #region Form Init/Load
        public ConfigurationForm()
        {
            InitializeComponent();
        }

        private void ConfigurationForm_Load(object sender, EventArgs e)
        {
            _introImage.Add(
                "http://i.imgur.com/MUc7BLm.jpg");


            var rand = new Random();
            var nextImage = rand.Next(0, _introImage.Count - 1);
            pictureBox1.ImageLocation = _introImage[nextImage];
            LoadFormSettings();

            changeLog.Text = ChangeLogText;
        }

        private void LoadFormSettings()
        {

            #region Items
            UseHealthstones.Checked = Settings.UseHealthstones;
            HealthstonePercent.Value = Settings.HealthstoneHPPercent;
            UseTrinket1.Checked = Settings.UseTrinket1;
            UseTrinket2.Checked = Settings.UseTrinket2;
            cmboTrinket1.SelectedIndex = Settings.Trinket1Condition;
            cmboTrinket2.SelectedIndex = Settings.Trinket2Condition;
            Trinket1LowHPPercent.Value = Settings.Trinket1LowHPValue;
            Trinket1LowManaPercent.Value = Settings.Trinket1LowManaValue;
            Trinket2LowHPPercent.Value = Settings.Trinket2LowHPValue;
            Trinket2LowManaPercent.Value = Settings.Trinket2LowManaValue;
            #endregion

            #region Racials
            cmboRacialUsage.SelectedIndex = Settings.RacialUsage;
            #endregion

            #region Talents

            // Tier 1
            VoidTendrilsTargetInRange.Checked = Settings.UseVoidTendrilsOnTargetInRange;

            PsyfiendOnCooldown.Checked = Settings.PsyfiendOnCooldown;

            // Tier 2
            UseBodyAndSoulInCombat.Checked = Settings.BodyAndSoulInCombat;
            UseBodyAndSoulNoCombat.Checked = Settings.BodyAndSoulNoCombat;

            UseAngelicFeatherInCombat.Checked = Settings.AngelicFeatherInCombat;
            UseAngelicFeatherNoCombat.Checked = Settings.AngelicFeatherNoCombat;

            PhantasmFadeWhenRootedSnared.Checked = Settings.FadeWhenSnaredRooted;

            // Tier 3
            UseMindbender.Checked = Settings.UseMindbender;

            // Tier 4
            UseDesperatePrayer.Checked = Settings.UseDesperatePrayer;
            DesperatePrayerHPValue.Value = Settings.DesperatePrayerHealthValue;

            UseSpectralGuiseOnLowHP.Checked = Settings.SpectralGuiseOnLowHP;
            SpectralGuiseLowHPValue.Value = Settings.SpectralGuiseLowHPValue;

            // Tier 5
            PowerInfustionCondition.SelectedIndex = Settings.PowerInfusionCondition;
            PowerInfusionLowHPValue.Value = Settings.PowerInfusionLowHPValue;

            // Tier 6
            UseCascade.Checked = Settings.UseCascade;
            UseDivineStar.Checked = Settings.UseDivineStar;
            UseHalo.Checked = Settings.UseHalo;

            #endregion

            #region Professions
            UseSynapseSprings.Checked = Settings.UseSynapseSprings;
            cmboSynapseSprings.SelectedIndex = Settings.SynapseSpringsCond;
            UseLifeblood.Checked = Settings.UseLifeblood;
            cmboLifeblood.SelectedIndex = Settings.LifebloodCond;
            LifebloodLowHPValue.Value = Settings.LifebloodLowHPValue;
            #endregion

            #region General - Non Specialization Abilities
            
            FadeToReduceThreat.Checked = Settings.UseFadeToReduceThreat;

            UseFearWard.Checked = Settings.UseFearWard;
            
            InnerFire.Checked = Settings.InnerFire;
            InnerWill.Checked = Settings.InnerWill;

            UsePoMLowHP.Checked = Settings.UsePoMLowHP;
            PoMLowHPValue.Value = Settings.PoMLowHPValue;
            UsePoMAlways.Checked = Settings.AlwaysKeepPoMActive;

            PsychicScreamTarget.Checked = Settings.PsychicScreamTarget;
            PsychicScreamAny.Checked = Settings.PsychicScreamAny;

            PowerWordShieldLowHP.Checked = Settings.UsePowerWordShieldLowHp;
            PowerWordShieldLowHPValue.Value = Settings.PowerWordShieldLowHPValue;

            RenewOnLowHP.Checked = Settings.UseRenewLowHp;
            RenewLowHPValue.Value = Settings.RenewLowHPValue;

            LeapOfFaithLowHPTarget.Checked = Settings.LeapOfFaithLowHP;
            LeapOfFaithLowHPValue.Value = Settings.LeapOfFaithLowHPValue;
            LeapOfFaithCrowdControlledUnit.Checked = Settings.LeapOfFaithCrowdControl;

            #endregion

            #region Shadow
            
            // AoE Controls
            Shadow_EnableAoE.Checked = Settings.Shadow_EnableAoEAbilities;
            Shadow_HighAoECount.Value = Settings.Shadow_AoEHighUnitCount;

            SWPForceRefreshTime.Value = Settings.Shadow_SWPForceRefreshTime;
            SWPMaxDoTsToCast.Value = Settings.Shadow_SWPMaxDoTsToCast;

            VTForceRefreshTime.Value = Settings.Shadow_VTForceRefreshTime;
            VTMaxDoTsToCast.Value = Settings.Shadow_VTMaxDoTsToCast;
            
            UseVampiricEmbrace.Checked = Settings.Shadow_UseVampiricEmbrace;
            VampiricEmbraceHealthPercent.Value = Settings.Shadow_VampiricEmbraceHealthPercent;
            
            Dispersion_LowHP.Checked = Settings.Shadow_Dispersion_LowHP;
            Dispersion_LowHPValue.Value = Settings.Shadow_Dispersion_LowHPValue;
            Dispersion_LowMana.Checked = Settings.Shadow_Dispersion_LowMana;
            Dispersion_LowManaValue.Value = Settings.Shadow_Dispersion_LowManaValue;
            
            #endregion

            #region GUI Stuff

            #endregion

            #region Other

            UsePowerWordFortitude.Checked = Settings.UsePowerWordFortitude;

            #endregion

        }

        private void StoreSettingsToInstance()
        {

            #region Items
            Settings.UseHealthstones = UseHealthstones.Checked;
            Settings.HealthstoneHPPercent = Convert.ToInt32(HealthstonePercent.Value);
            Settings.UseTrinket1 = UseTrinket1.Checked;
            Settings.UseTrinket2 = UseTrinket2.Checked;
            Settings.Trinket1Condition = cmboTrinket1.SelectedIndex;
            Settings.Trinket2Condition = cmboTrinket2.SelectedIndex;
            Settings.Trinket1LowHPValue = Convert.ToInt32(Trinket1LowHPPercent.Value);
            Settings.Trinket1LowManaValue = Convert.ToInt32(Trinket1LowManaPercent.Value);
            Settings.Trinket2LowHPValue = Convert.ToInt32(Trinket2LowHPPercent.Value);
            Settings.Trinket2LowManaValue = Convert.ToInt32(Trinket2LowManaPercent.Value);
            #endregion
            
            #region Professions
            Settings.SynapseSpringsCond = cmboSynapseSprings.SelectedIndex;
            Settings.UseSynapseSprings = UseSynapseSprings.Checked;
            Settings.UseLifeblood = UseLifeblood.Checked;
            Settings.LifebloodCond = cmboLifeblood.SelectedIndex;
            Settings.LifebloodLowHPValue = Convert.ToInt32(LifebloodLowHPValue.Value);
            #endregion

            #region Racials
            Settings.RacialUsage = cmboRacialUsage.SelectedIndex;
            #endregion

            #region Talents
            // Tier 1
            Settings.UseVoidTendrilsOnTargetInRange = VoidTendrilsTargetInRange.Checked;

            Settings.PsyfiendOnCooldown = PsyfiendOnCooldown.Checked;

            // Tier 2
            Settings.BodyAndSoulInCombat = UseBodyAndSoulInCombat.Checked;
            Settings.BodyAndSoulNoCombat = UseBodyAndSoulNoCombat.Checked;

            Settings.AngelicFeatherInCombat = UseAngelicFeatherInCombat.Checked;
            Settings.AngelicFeatherNoCombat = UseAngelicFeatherNoCombat.Checked;

            Settings.FadeWhenSnaredRooted = PhantasmFadeWhenRootedSnared.Checked;

            // Tier 3
            Settings.UseMindbender = UseMindbender.Checked;

            // Tier 4
            Settings.UseDesperatePrayer = UseDesperatePrayer.Checked;
            Settings.DesperatePrayerHealthValue = Convert.ToInt32(DesperatePrayerHPValue.Value);

            Settings.SpectralGuiseOnLowHP = UseSpectralGuiseOnLowHP.Checked;
            Settings.SpectralGuiseLowHPValue = Convert.ToInt32(SpectralGuiseLowHPValue.Value);

            // Tier 5
            Settings.PowerInfusionCondition = PowerInfustionCondition.SelectedIndex;
            Settings.PowerInfusionLowHPValue = Convert.ToInt32(PowerInfusionLowHPValue.Value);

            // Tier 6
            Settings.UseCascade = UseCascade.Checked;
            Settings.UseDivineStar = UseDivineStar.Checked;
            Settings.UseHalo = UseHalo.Checked;

            #endregion

            #region General - Non Specialization Abilities
            
            Settings.UseFadeToReduceThreat = FadeToReduceThreat.Checked;

            Settings.UseFearWard = UseFearWard.Checked;

            Settings.InnerFire = InnerFire.Checked;
            Settings.InnerWill = InnerWill.Checked;

            Settings.UsePoMLowHP = UsePoMLowHP.Checked;
            Settings.PoMLowHPValue = Convert.ToInt32(PoMLowHPValue.Value);
            Settings.AlwaysKeepPoMActive = UsePoMAlways.Checked;

            Settings.PsychicScreamTarget = PsychicScreamTarget.Checked;
            Settings.PsychicScreamAny = PsychicScreamAny.Checked;

            Settings.UsePowerWordShieldLowHp = PowerWordShieldLowHP.Checked;
            Settings.PowerWordShieldLowHPValue = Convert.ToInt32(PowerWordShieldLowHPValue.Value);

            Settings.UseRenewLowHp = RenewOnLowHP.Checked;
            Settings.RenewLowHPValue = Convert.ToInt32(RenewLowHPValue.Value);

            Settings.LeapOfFaithLowHP = LeapOfFaithLowHPTarget.Checked;
            Settings.LeapOfFaithLowHPValue = Convert.ToInt32(LeapOfFaithLowHPValue.Value);
            Settings.LeapOfFaithCrowdControl = LeapOfFaithCrowdControlledUnit.Checked;

            #endregion

            #region Shadow
            // AoE Controls
            Settings.Shadow_EnableAoEAbilities = Shadow_EnableAoE.Checked;
            Settings.Shadow_AoEHighUnitCount = Convert.ToInt32(Shadow_HighAoECount.Value);

            Settings.Shadow_SWPForceRefreshTime = Convert.ToInt32(SWPForceRefreshTime.Value);
            Settings.Shadow_SWPMaxDoTsToCast = Convert.ToInt32(SWPMaxDoTsToCast.Value);

            Settings.Shadow_VTForceRefreshTime = Convert.ToInt32(VTForceRefreshTime.Value);
            Settings.Shadow_VTMaxDoTsToCast = Convert.ToInt32(VTMaxDoTsToCast.Value);

            Settings.Shadow_UseVampiricEmbrace = UseVampiricEmbrace.Checked;
            Settings.Shadow_VampiricEmbraceHealthPercent = Convert.ToInt32(VampiricEmbraceHealthPercent.Value);

            Settings.Shadow_Dispersion_LowHP = Dispersion_LowHP.Checked;
            Settings.Shadow_Dispersion_LowHPValue = Convert.ToInt32(Dispersion_LowHPValue.Value);
            Settings.Shadow_Dispersion_LowMana = Dispersion_LowMana.Checked;
            Settings.Shadow_Dispersion_LowManaValue = Convert.ToInt32(Dispersion_LowManaValue.Value);
            
            #endregion

            #region Other
            Settings.UsePowerWordFortitude = UsePowerWordFortitude.Checked;

            #endregion

        }
        #endregion

        #region Button Hover Stuff
        private void btnSave_MouseEnter(object sender, EventArgs e)
        {
            ButtonHoverOver((Control)sender);
        }

        private void btnSave_MouseLeave(object sender, EventArgs e)
        {
            ButtonLeave((Control)sender);
        }

        private void btnCancel_MouseEnter(object sender, EventArgs e)
        {
            ButtonHoverOver(btnCancel);
        }

        private void btnCancel_MouseLeave(object sender, EventArgs e)
        {
            ButtonLeave(btnCancel);
        }

        private void btnLoadFromFile_MouseEnter(object sender, EventArgs e)
        {
            ButtonHoverOver(btnLoadFromFile);
        }

        private void btnLoadFromFile_MouseLeave(object sender, EventArgs e)
        {
            ButtonLeave(btnLoadFromFile);
        }

        private void btnSaveToFile_MouseEnter(object sender, EventArgs e)
        {
            ButtonHoverOver(btnSaveToFile);
        }

        private void btnSaveToFile_MouseLeave(object sender, EventArgs e)
        {
            ButtonLeave(btnSaveToFile);
        }

        private void ButtonHoverOver(Control button)
        {
            button.BackColor = Color.MediumPurple;
            button.ForeColor = Color.White;
        }

        private void ButtonLeave(Control button)
        {
            button.BackColor = Color.Gray;
            button.ForeColor = Color.White;
        }

        private void ButtonClick(Control button)
        {
            button.BackColor = Color.White;
            button.ForeColor = Color.Purple;
        }



        #endregion

        #region Button Click Actions
        private void btnSave_Click(object sender, EventArgs e)
        {
            ButtonClick(btnSave);
            StoreSettingsToInstance();
            Settings.Save();
            Settings.NonStaticLogSettings();
            Close();
            ButtonLeave(btnSave);
            //Rep();
        }

        private static void Rep()
        {
            if (Settings.HasGivenRep != 0) return;
            
            var result = MessageBox.Show("Do you love Insanity? If so, please add to my reputation power by clicking Yes! (You won't be shown this message again)", "Insanity", 
                MessageBoxButtons.YesNo);

            if (result == DialogResult.Yes)
            {
                Settings.HasGivenRep = 1;
                Process.Start("http://www.thebuddyforum.com/reputation.php?do=addreputation&p=1217644"); // Portal = 1250413
            }
            else
            {
                Settings.HasGivenRep = 2;
            }

            Settings.Save();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ButtonClick(btnCancel);
            Settings.Load();
            Close();
            ButtonLeave(btnCancel);
        }

        private void btnLoadFromFile_Click(object sender, EventArgs e)
        {
            ButtonClick(btnLoadFromFile);
            InsanitySettings.LoadFile();
            LoadFormSettings();
            ButtonLeave(btnLoadFromFile);
        }

        private void btnSaveToFile_Click(object sender, EventArgs e)
        {
            ButtonClick(btnSaveToFile);
            StoreSettingsToInstance();
            InsanitySettings.SaveFile();
            ButtonLeave(btnSaveToFile);
        }
        

        private void RefreshTimeDescription_Click(object sender, EventArgs e)
        {
            MessageBox.Show(@"This value is the minimum time left on the DoT (in milliseconds) before forcing a refresh.

This timer will be hit when our current stats are the same, or lower than those applied to the target.");
        }


        private void MaxDoTsToCastDescription_Click(object sender, EventArgs e)
        {
            MessageBox.Show(@"This value is the maximum amount of the DoTs to cast when multi-DoTing.");
        }

        private void SWPForceRefreshTime_ValueChanged(object sender, EventArgs e)
        {

        }
        
        #endregion

        #region State Change - Enable/Disable Components
        
        private void cmboQuickLoad_DropDown(object sender, EventArgs e)
        {
            ComboBox senderComboBox = (ComboBox)sender;
            int width = senderComboBox.DropDownWidth;
            Graphics g = senderComboBox.CreateGraphics();
            Font font = senderComboBox.Font;
            int vertScrollBarWidth =
                (senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
                ? SystemInformation.VerticalScrollBarWidth : 0;

            int newWidth;
            foreach (string s in ((ComboBox)sender).Items)
            {
                newWidth = (int)g.MeasureString(s, font).Width
                    + vertScrollBarWidth;
                if (width < newWidth)
                {
                    width = newWidth;
                }
            }
            senderComboBox.DropDownWidth = width;
        }

        private void cmboHotkey_Movement_DropDown(object sender, EventArgs e)
        {
            ComboBox senderComboBox = (ComboBox)sender;
            int width = senderComboBox.DropDownWidth;
            Graphics g = senderComboBox.CreateGraphics();
            Font font = senderComboBox.Font;
            int vertScrollBarWidth =
                (senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
                ? SystemInformation.VerticalScrollBarWidth : 0;

            int newWidth;
            foreach (string s in ((ComboBox)sender).Items)
            {
                newWidth = (int)g.MeasureString(s, font).Width
                    + vertScrollBarWidth;
                if (width < newWidth)
                {
                    width = newWidth;
                }
            }
            senderComboBox.DropDownWidth = width;
        }

        private void cmboHotkey_Targeting_DropDown(object sender, EventArgs e)
        {
            ComboBox senderComboBox = (ComboBox)sender;
            int width = senderComboBox.DropDownWidth;
            Graphics g = senderComboBox.CreateGraphics();
            Font font = senderComboBox.Font;
            int vertScrollBarWidth =
                (senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
                ? SystemInformation.VerticalScrollBarWidth : 0;

            int newWidth;
            foreach (string s in ((ComboBox)sender).Items)
            {
                newWidth = (int)g.MeasureString(s, font).Width
                    + vertScrollBarWidth;
                if (width < newWidth)
                {
                    width = newWidth;
                }
            }
            senderComboBox.DropDownWidth = width;
        }

        private void cmboHotkey_Facing_DropDown(object sender, EventArgs e)
        {
            ComboBox senderComboBox = (ComboBox)sender;
            int width = senderComboBox.DropDownWidth;
            Graphics g = senderComboBox.CreateGraphics();
            Font font = senderComboBox.Font;
            int vertScrollBarWidth =
                (senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
                ? SystemInformation.VerticalScrollBarWidth : 0;

            int newWidth;
            foreach (string s in ((ComboBox)sender).Items)
            {
                newWidth = (int)g.MeasureString(s, font).Width
                    + vertScrollBarWidth;
                if (width < newWidth)
                {
                    width = newWidth;
                }
            }
            senderComboBox.DropDownWidth = width;
        }

        private void cmboHotkey_AoE_DropDown(object sender, EventArgs e)
        {
            ComboBox senderComboBox = (ComboBox)sender;
            int width = senderComboBox.DropDownWidth;
            Graphics g = senderComboBox.CreateGraphics();
            Font font = senderComboBox.Font;
            int vertScrollBarWidth =
                (senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
                ? SystemInformation.VerticalScrollBarWidth : 0;

            int newWidth;
            foreach (string s in ((ComboBox)sender).Items)
            {
                newWidth = (int)g.MeasureString(s, font).Width
                    + vertScrollBarWidth;
                if (width < newWidth)
                {
                    width = newWidth;
                }
            }
            senderComboBox.DropDownWidth = width;
        }

        private void cmboHotkey_Burst_DropDown(object sender, EventArgs e)
        {
            ComboBox senderComboBox = (ComboBox)sender;
            int width = senderComboBox.DropDownWidth;
            Graphics g = senderComboBox.CreateGraphics();
            Font font = senderComboBox.Font;
            int vertScrollBarWidth =
                (senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
                ? SystemInformation.VerticalScrollBarWidth : 0;

            int newWidth;
            foreach (string s in ((ComboBox)sender).Items)
            {
                newWidth = (int)g.MeasureString(s, font).Width
                    + vertScrollBarWidth;
                if (width < newWidth)
                {
                    width = newWidth;
                }
            }
            senderComboBox.DropDownWidth = width;
        }

        private void cmboBanish_DropDown(object sender, EventArgs e)
        {
            ComboBox senderComboBox = (ComboBox)sender;
            int width = senderComboBox.DropDownWidth;
            Graphics g = senderComboBox.CreateGraphics();
            Font font = senderComboBox.Font;
            int vertScrollBarWidth =
                (senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
                ? SystemInformation.VerticalScrollBarWidth : 0;

            int newWidth;
            foreach (string s in ((ComboBox)sender).Items)
            {
                newWidth = (int)g.MeasureString(s, font).Width
                    + vertScrollBarWidth;
                if (width < newWidth)
                {
                    width = newWidth;
                }
            }
            senderComboBox.DropDownWidth = width;
        }

        #endregion State Change - Enable/Disable Components

    }
}
